package com.example.administrator.im.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.example.administrator.im.Contents;
import com.example.administrator.im.R;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：wp
 * 时间：2016/4/19 16:33
 * 描述：TODO
 */
public class ApplyAndNotificationAdapter extends BaseAdapter {

    private Context context;
    private List<Map<String, Object>> list;
    private btnOnClick click;

    public ApplyAndNotificationAdapter(Context context) {
        this.context = context;
        this.list = Contents.lists;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.apply_notifi_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String username = (String) list.get(position).get("add_username");
        holder.tvUsername.setText(username);
        holder.tvReason.setText((CharSequence) list.get(position).get("add_reason"));
        holder.btAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click.onClickAgreeBtn(username,holder.btAgree,holder.btRefuse,holder.tvHint);
            }
        });
        holder.btRefuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click.onClickRefuseBtn(username,holder.btAgree,holder.btRefuse,holder.tvHint);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tv_username)
        TextView tvUsername;
        @Bind(R.id.tv_reason)
        TextView tvReason;
        @Bind(R.id.bt_agree)
        Button btAgree;
        @Bind(R.id.bt_refuse)
        Button btRefuse;
        @Bind(R.id.tv_hint)
        TextView tvHint;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void setOnClickBtnListener(btnOnClick click) {
        this.click = click;
    }

    //按钮点击回调接口
    public interface btnOnClick{
        void onClickAgreeBtn(String username,Button btn1,Button btn2,TextView tv);
        void onClickRefuseBtn(String username,Button btn1,Button btn2,TextView tv);
    }
}
